package com.dede.applib.util

import android.util.Log
import com.dede.applib.BuildConfig

/**
 * @author hsh
 * @date 2017/8/17 14:29.
 * @doc 日志扩展
 */
interface LogExtends {

    val loggerTag: String
        get() {
            val tag = javaClass.simpleName
            return if (tag.length <= 23) {
                tag
            } else {
                tag.substring(0, 23)
            }
        }

    val loggerEnable: Boolean
        get() {
            return BuildConfig.DEBUG
        }
}

fun LogExtends.v(message: Any?, thr: Throwable? = null) {
    if (loggerEnable)
        Log.v(loggerTag, message.toString(), thr)
}

fun LogExtends.v(message: () -> Any?, throwable: (() -> Throwable?)? = null) {
    this.v(message(), throwable?.invoke())
}

fun LogExtends.i(message: Any?, thr: Throwable? = null) {
    if (loggerEnable)
        Log.i(loggerTag, message.toString(), thr)
}

fun LogExtends.i(message: () -> Any?, throwable: (() -> Throwable?)? = null) {
    this.i(message(), throwable?.invoke())
}

fun LogExtends.d(message: Any?, thr: Throwable? = null) {
    if (loggerEnable)
        Log.d(loggerTag, message.toString(), thr)
}

fun LogExtends.d(message: () -> Any?, throwable: (() -> Throwable?)? = null) {
    this.d(message(), throwable?.invoke())
}

fun LogExtends.w(message: Any?, thr: Throwable? = null) {
    if (loggerEnable)
        Log.w(loggerTag, message.toString(), thr)
}

fun LogExtends.w(message: () -> Any?, throwable: (() -> Throwable?)? = null) {
    this.w(message(), throwable?.invoke())
}

fun LogExtends.e(message: Any?, thr: Throwable? = null) {
    if (loggerEnable)
        Log.e(loggerTag, message.toString(), thr)
}

fun LogExtends.e(message: () -> Any?, throwable: (() -> Throwable?)? = null) {
    this.e(message(), throwable?.invoke())
}

fun LogExtends.wtf(message: Any?, thr: Throwable? = null) {
    if (loggerEnable)
        Log.wtf(loggerTag, message.toString(), thr)
}

fun LogExtends.wtf(message: () -> Any?, throwable: (() -> Throwable?)? = null) {
    this.wtf(message(), throwable?.invoke())
}
